package com.xtravel.common.helper;

import android.annotation.SuppressLint;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@SuppressLint("DefaultLocale")
public class LangUtil {

    public static Field[] getFields(Class<?> adpater) {
        return adpater.getDeclaredFields();
    }

    public static void setValue(Object bean, String fieldName, Object oval) {
        try {
            Field field = bean.getClass().getField(fieldName);
            field.set(bean, oval);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Class<?> getWrapperClass(Class<?> classz) {
        if (classz.getClass().equals(int.class)) {
            return Integer.class;
        }
        if (classz.getClass().equals(double.class)) {
            return Double.class;
        }
        if (classz.getClass().equals(float.class)) {
            return Float.class;
        }

        return classz;
    }

    public static Method getGetter(Class<?> class1, String fieldName) throws NoSuchMethodException {
        try {
            return class1.getDeclaredMethod("get" + captureName(fieldName));
        } catch (Exception e) {
            return null;
        }
    }

    public static Method getSetter(Class<?> class1, String fieldName, Class<?> type) throws NoSuchMethodException {
        try {
            return class1.getDeclaredMethod("set" + captureName(fieldName), type);
        } catch (Exception e) {
            return null;
        }
    }

    private static String captureName(String name) {
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        return name;
    }

    public static Field getField(Class<?> adpater, String key) {
        try {
            return adpater.getDeclaredField(key);
        } catch (NoSuchFieldException e) {
            return null;
        }
    }

}
